/**
 * 
 */
package com.itinsys.enma.dao.impl;

import java.util.ArrayList;
import java.util.List;

import net.sf.ehcache.ObjectExistsException;

import org.apache.log4j.Logger;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.type.Type;

import com.itinsys.enma.dao.DBTemplate;
import com.itinsys.enma.dao.ResourceDAO;
import com.itinsys.enma.dao.hibernate.DBObject;
import com.itinsys.enma.domain.Environment;
import com.itinsys.enma.domain.Resource;

/**
 * @author pferraz
 *
 */
public class ResourceDAOImpl implements ResourceDAO{

	private static Logger logger=Logger.getLogger("dao");
	
	@SuppressWarnings("unchecked")
	@Override
	public Resource getById(int id) {
		List<Resource> result=null;
		Session sess=null;
		try{
			sess=DBTemplate.getConnTemplate().getSessionFactory().openSession();
			Query q=sess.createQuery("from r in class com.itinsys.enma.domain.Resource where r.id=?");
			q.setParameters(new Object[]{id}, new Type[]{Hibernate.INTEGER});
			result=q.list();
			if(result.isEmpty()){
				throw new ObjectExistsException("El recurso ingresado no existe");	
			}
		}catch(HibernateException e){
			logger.error("Error al buscar recurso por identificador. " ,e);
		}finally {
            if (sess != null){
            	sess.close();	
            }
        }
		return result.get(0);
	}

	@Override
	public boolean addResource(Resource r) {

		return DBObject.addObject(r);
		
	}
	
	@Override
	public boolean deleteResource(Resource r) {
		
		return DBObject.removeObject(r);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Resource> getAll() {
		List<Resource> result=null;
		Session sess=null;
		try{
			sess=DBTemplate.getConnTemplate().getSessionFactory().openSession();
			Query q=sess.createQuery("from s in class com.itinsys.enma.domain.Resource");
			result=q.list();
			
		}catch(HibernateException e){
			logger.error("Error al buscar el recurso por identificador. " ,e);
		}finally {
            if (sess != null){
            	sess.close();	
            }
        }
		return result;
	}

	@Override
	public boolean updateResourcel(Resource resource) {
		// TODO Auto-generated method stub
		return DBObject.updateObject(resource);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Resource getByDesc(String description) {
		List<Resource> result=null;
		Session sess=null;
		try{
			sess=DBTemplate.getConnTemplate().getSessionFactory().openSession();
			Query q=sess.createQuery("from s in class com.itinsys.enma.domain.Resource where s.description=?");
			q.setParameters(new Object[]{description}, new Type[]{Hibernate.STRING});
			result=q.list();
			if(result.isEmpty()){
				throw new ObjectExistsException("La facultad ingresada no existe");	
			}
		}catch(HibernateException e){
			logger.error("Error al buscar recurso por identificador. " ,e);
		}finally {
            if (sess != null){
            	sess.close();	
            }
        }
		return result.get(0);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Environment> getEnvironmentByResource(int resourceId) {
		Session sess = null;
		Query query=null;
		List<Environment> result = new ArrayList<Environment>();
		try {
			sess = DBTemplate.getConnTemplate().getSessionFactory().openSession();
			query = sess.createQuery("select e, r from com.itinsys.enma.domain.Environment e inner join fetch e.resources r where r.id=?");
			query.setParameters(new Object[] {resourceId}, new Type[] {Hibernate.INTEGER});
			result = query.list();
			
		} catch (HibernateException e) {
			logger.error("Error al buscar ambientes por recurso", e);
		} finally {
			if (sess != null) {
				sess.close();
			}

		}
		return result;
	}
	
}
